
module samples.base.collections;

import dotmars.base.iterator;
import dotmars.base.collection.list;
import dotmars.base.collection.array;
import dotmars.base.algorithms;
import dotmars.io.console;

void main()
{
	alias List!(int) MyList;

	auto list = new MyList;

	list ~= 1; // this is equal to list.addList(1);
	list ~= 2;
	list ~= 3;
	list ~= 4;
	list ~= 5;
	list ~= 6;
	list.addFirst(7);

	//现在序列为： 7,1,2,3,4,5,6
	
	MyList.Iterator it = list.begin();
	++it;
	++it;

	//删除2, it 指向3
	it = list.remove(it);
	
	//现在序列为： 7,2,3,4,5,6

	list.addBefore(it, 2); //在3之前插入2，it 指向 3


	int[] array;
	array.addLast(8);
	array.addLast(9);

	// 内置数组也可以用迭代器访问
	list.addRangeBefore(it, array.begin(), array.end()); //在3之前插入array的内容，即 8,9

	//使用 foreach 语句遍历 list
	foreach(int i; list)
		Console.print("{0}, ", i);

	Console.newLine();

	//与 STL 类似，通过内部的迭代器反向遍历 list
	for(MyList.ReverseIterator ri = list.rbegin(); ri != list.rend(); ++ri)
		Console.print("{0}, ", ri.value);

	Console.newLine();

	//调用通用算法
	MyList.Iterator pos = find(list.begin(), list.end(), 5);
	
	if(pos != list.end())
		Console.printLine("Pattern found: {0}", pos.value);

	list.clear();
}
